/*
package com.example.newwork

import androidx.annotation.MainThread
import androidx.annotation.WorkerThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

abstract class NetWorkResource<RequestType,ResultType> {
    private val mediatorLiveData = MediatorLiveData<NetResource<ResultType>>()
    init {
        mediatorLiveData.value =NetResource.doLoading<ResultType>(null)
        fetchFromNetWork()

    }

    @MainThread
    private fun setValue(newValue:NetResource<ResultType>){
        if (mediatorLiveData.value!=newValue){
            mediatorLiveData.value = newValue
        }
    }
    private  fun fetchFromNetWork(){
        val  response = convertData()
        setValue(NetResource.doLoading<ResultType>(null))
        mediatorLiveData.addSource(response){ res->
            mediatorLiveData.removeSource(response)
           when(res){
                is NetResource.doSuccess<RequestType> ->{
                    GlobalScope.launch(Dispatchers.IO) {
                        mediatorLiveData.addSource(object :LiveData<NetResource<ResultType>>(){
                              init {
                                 postValue(fetchNet(res))
                              }
                        }){
                            setValue(it)
                        }
                    }
               }
               is NetResource.doFailure->{

               }
               is NetResource.doLoading ->{

               }
           }
        }

    }
    @WorkerThread
    abstract  fun fetchNet(requestType: NetResource.doSuccess<RequestType>):NetResource<ResultType>
    @MainThread
    abstract fun convertData():LiveData<NetResource<RequestType>>
    fun asLiveData() = mediatorLiveData as LiveData<NetResource<ResultType>>



    fun asFlow():Flow<NetResource<ResultType>>{
           return  flow {
                 mediatorLiveData.value?.let { emit(it) }
             }
    }
}*/
